package ru.nsu.fit.ws.authentication;

import ru.nsu.fit.shared.data_base_manager.DatabaseManager;

import javax.jws.HandlerChain;
import javax.jws.WebService;

/**
 * @author Sergey Kozlov
 * 		   Timur Zolotuhin (tzolotuhin@gmail.com)
 */
@WebService(endpointInterface = "ru.nsu.fit.ws.authentication.Authentication")
@HandlerChain(file= "handler-chain.xml")
public class AuthenticationImpl implements Authentication{
    @Override
    public AuthenticationOutput authenticate(final AuthenticationInput input) {
        if (input == null) return null;
        
        AuthenticationOutput output = new AuthenticationOutput();
        String login = input.getLogin();
        String pass = input.getPassword();
        try {
        	long userSessionId = DatabaseManager.authenticate(login, pass);
    		output.setUserSessionId(userSessionId);
            output.setSuccessful(true);
        } catch (Exception ex) {
        	output.setSuccessful(false);
        	output.setErrorMessage(ex.getMessage());
        }
        return output;
    }
}
